package cn.stylefeng.guns.modular.file.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.roses.kernel.rule.exception.base.ServiceException;
import cn.stylefeng.guns.modular.file.entity.File;
import cn.stylefeng.guns.modular.file.enums.FileExceptionEnum;
import cn.stylefeng.guns.modular.file.mapper.FileMapper;
import cn.stylefeng.guns.modular.file.pojo.request.FileRequest;
import cn.stylefeng.guns.modular.file.service.FileService;
import cn.stylefeng.roses.kernel.db.api.factory.PageFactory;
import cn.stylefeng.roses.kernel.db.api.factory.PageResultFactory;
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 业务实现层
 *
 * @author xbz
 * @date 2023/04/20 15:15
 */
@Service
public class FileServiceImpl extends ServiceImpl<FileMapper, File> implements FileService {

	@Override
    public void add(FileRequest fileRequest) {
        File file = new File();
        BeanUtil.copyProperties(fileRequest, file);
        this.save(file);
    }

    @Override
    public void del(FileRequest fileRequest) {
        File file = this.queryFile(fileRequest);
        this.removeById(file.getId());
    }

    @Override
    public void edit(FileRequest fileRequest) {
        File file = this.queryFile(fileRequest);
        BeanUtil.copyProperties(fileRequest, file);
        this.updateById(file);
    }

    @Override
    public File detail(FileRequest fileRequest) {
        return this.queryFile(fileRequest);
    }

    @Override
    public PageResult<File> findPage(FileRequest fileRequest) {
        LambdaQueryWrapper<File> wrapper = createWrapper(fileRequest);
        Page<File> sysRolePage = this.page(PageFactory.defaultPage(), wrapper);
        return PageResultFactory.createPageResult(sysRolePage);
    }

    @Override
    public List<File> findList(FileRequest fileRequest) {
        LambdaQueryWrapper<File> wrapper = this.createWrapper(fileRequest);
        return this.list(wrapper);
    }

    /**
     * 获取信息
     *
     * @author xbz
     * @date 2023/04/20 15:15
     */
    private File queryFile(FileRequest fileRequest) {
        File file = this.getById(fileRequest.getId());
        if (ObjectUtil.isEmpty(file)) {
            throw new ServiceException(FileExceptionEnum.FILE_NOT_EXISTED);
        }
        return file;
    }

    /**
     * 创建查询wrapper
     *
     * @author xbz
     * @date 2023/04/20 15:15
     */
    private LambdaQueryWrapper<File> createWrapper(FileRequest fileRequest) {
        LambdaQueryWrapper<File> queryWrapper = new LambdaQueryWrapper<>();

        Integer id = fileRequest.getId();
        Integer pid = fileRequest.getPid();
        String name = fileRequest.getName();
        String url = fileRequest.getUrl();

        queryWrapper.eq(ObjectUtil.isNotNull(id), File::getId, id);
        queryWrapper.eq(ObjectUtil.isNotNull(pid), File::getPid, pid);
        queryWrapper.like(ObjectUtil.isNotEmpty(name), File::getName, name);
        queryWrapper.like(ObjectUtil.isNotEmpty(url), File::getUrl, url);

        return queryWrapper;
    }

}